﻿using GetTogether.Studio.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Logs : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        object ret = null;
        //SaveLoggings("http://127.0.0.1", "http://127.0.0.1/Logs/");
        //SaveLoggings("http://10.100.106.47:10001", "http://10.100.106.47:10001/logs/Adholidays/WTL/B2B(Soap)/2013-04-23/");
        //SaveLoggings("https://dragonfly.opera.com/app/core-2-5/cutting-edge", "https://dragonfly.opera.com/app/core-2-5/cutting-edge/zips/2012.11.21.46a22a845e72/");
        //Studio.Go.Index();
        //Response.Write("Success");
        //Response.Write(GetTogether.Utility.SerializationHelper.SerializeToXml(GetLoggings("https://dragonfly.opera.com/app/core-2-5/cutting-edge/zips/")));
        //DownloadFiles dFile = GetDownloadFiles("https://dragonfly.opera.com/app/core-2-5/cutting-edge", "https://dragonfly.opera.com/app/core-2-5/cutting-edge/zips/2012.11.21.46a22a845e72/");
        //ret = GetDownloadFiles("http://10.100.106.47:10001", "http://10.100.106.47:10001/logs/Adholidays/WTL/B2B(Soap)/2013-04-24/");

        //ret = GetTogether.Studio.Logging.Util.GetLoggings("http://127.0.0.1/Logs");
        SaveLoggings("http://10.100.111.81:10021/logs", "http://10.100.111.81:10021/logs/Download/20130822/");
        Response.Write(GetTogether.Utility.SerializationHelper.SerializeToXml(ret));
        Response.ContentType = "text/plain";
        Response.End();
    }

    public class DownloadFiles : GetTogether.ObjectBase.ListBase<DownloadFiles.DownloadFile>
    {
        public DownloadFiles()
        {

        }
        public class DownloadFile
        {
            public string File;
            public string Url;
            public DownloadFile()
            {

            }
            public DownloadFile(string file, string url)
            {
                this.File = file;
                this.Url = url;
            }
        }
    }

    public static DownloadFiles GetDownloadFiles(string baseUrl, string url)
    {
        DownloadFiles dFiles = new DownloadFiles();
        string logPath = System.Web.HttpContext.Current.Server.MapPath("~/Logging/Files");
        Loggings logs = GetTogether.Studio.Logging.Util.GetLoggings(url);
        if (logs != null)
        {
            foreach (Loggings.Logging l in logs)
            {
                if (l.IsFile) continue;
                string currentPath = l.Href;
                string currentBaseUrl = baseUrl;
                if (!l.Href.StartsWith("/"))
                {
                    currentBaseUrl = url;
                    currentPath = string.Concat("\\", url.Replace(baseUrl, ""));
                    if (l.IsFile)
                    {
                        currentPath = string.Concat(currentPath, l.Href);
                    }
                }

                DownloadFiles df = GetDownloadFiles(baseUrl, string.Concat(currentBaseUrl, l.Href));
                if (df != null && df.Count > 0)
                {
                    dFiles.AddRange(df.ToArray());
                }

            }
            foreach (Loggings.Logging l in logs)
            {
                if (!l.IsFile || l.IsCodeFile) continue;
                //if (l.Date.Date != DateTime.Now.Date) continue;
                if (!string.IsNullOrEmpty(l.Size) && l.Size == "0") continue;
                if (string.IsNullOrEmpty(l.Href)) continue;
                string currentPath = l.Href;
                string currentBaseUrl = baseUrl;
                if (!l.Href.StartsWith("/"))
                {
                    currentBaseUrl = url;
                    currentPath = string.Concat("\\", url.Replace(baseUrl, ""));
                    if (l.IsFile)
                    {
                        currentPath = string.Concat(currentPath, l.Href);
                    }
                }
                string file = string.Concat(logPath, System.Web.HttpUtility.UrlDecode(currentPath).Replace("/", "\\"));
                System.IO.FileInfo fi = new FileInfo(file);
                string fileUrl = string.Concat(currentBaseUrl, l.Href);
                dFiles.Add(new DownloadFiles.DownloadFile(file, fileUrl));
            }
        }
        return dFiles;
    }

    public static void SaveLoggings(string baseUrl, string url)
    {
        string logPath = System.Web.HttpContext.Current.Server.MapPath("~/Logging/Files");
        Loggings logs = GetTogether.Studio.Logging.Util.GetLoggings(url);
        if (logs != null)
        {
            foreach (Loggings.Logging l in logs)
            {
                if (l.IsFile) continue;
                string currentPath = l.Href;
                string currentBaseUrl = baseUrl;
                if (!l.Href.StartsWith("/"))
                {
                    currentBaseUrl = url;
                    currentPath = string.Concat("\\", url.Replace(baseUrl, ""));
                    if (l.IsFile)
                    {
                        currentPath = string.Concat(currentPath, l.Href);
                    }
                }

                SaveLoggings(baseUrl, string.Concat(currentBaseUrl, l.Href));

            }
            foreach (Loggings.Logging l in logs)
            {
                if (!l.IsFile || l.IsCodeFile) continue;
                if (l.Date.Date != DateTime.Now.Date) continue;
                if (!string.IsNullOrEmpty(l.Size) && l.Size == "0") continue;
                if (string.IsNullOrEmpty(l.Href)) continue;
                string currentPath = l.Href;
                string currentBaseUrl = baseUrl;
                if (!l.Href.StartsWith("/"))
                {
                    currentBaseUrl = url;
                    currentPath = string.Concat("\\", url.Replace(baseUrl, ""));
                    if (l.IsFile)
                    {
                        currentPath = string.Concat(currentPath, l.Href);
                    }
                }
                string file = string.Concat(logPath, System.Web.HttpUtility.UrlDecode(currentPath).Replace("/", "\\"));
                System.IO.FileInfo fi = new FileInfo(file);
                string fileUrl = string.Concat(currentBaseUrl, l.Href);
                if (!System.IO.Directory.Exists(fi.DirectoryName)) System.IO.Directory.CreateDirectory(fi.DirectoryName);
                System.Threading.Thread td = new System.Threading.Thread(delegate()
                {
                    GetTogether.Net.Extension.WebClient wc = new GetTogether.Net.Extension.WebClient();
                    wc.Timeout = int.MaxValue / 1000;
                    wc.DownloadFile(fileUrl, file);
                });
                td.Start();

            }
        }
    }
}

